using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class HomeController : UI.BaseController
    {
        [UI.LoginValidate]
        public ActionResult Index()
        {
            var auth = new BLL.Authority();
            ViewBag.WebSites = auth.GetUserWebSite(BLL.LoginUser.LoginManager.UserName);
            ViewBag.ChatView = BLL.SystemConfig.LoadConfig().Feature.IsChat
            && auth.ValidateButtonAuthority(Common.Keys.WebSiteCode, "Chat","received", BLL.LoginUser.LoginManager.UserName).success;
            //ViewBag.EasyUIThemes = "black";
            return View();
        }

        [UI.LoginValidate]
        public ActionResult Desktop()
        {
            return View();
        }

        public ActionResult Login()
        {
            ViewBag.UserName = Common.Utils.GetCookie("RememberLoginUserName");
            ViewBag.Remember = Common.Utils.GetCookie("RememberLogin");
            if (string.IsNullOrEmpty(ViewBag.Remember)) ViewBag.Remember = true;
            return View();
        }

        /// <summary>
        /// 记住用户名
        /// </summary>
        private void RemmenberUser(string UserName,bool? Remember)
        {
            if (Remember != true)
            {
                UserName = "";
            }
            Common.Utils.WriteCookie("RememberLoginUserName", UserName, -1, Request.Path );
            Common.Utils.WriteCookie("RememberLogin", Remember.ToString().ToLower(), -1, Request.Path);
        }

        [HttpPost]
        public string Login(string UserName, string Password, string Code, bool? Remember)
        {
            UserName = UserName.ToUpper();
            RemmenberUser(UserName, Remember);

            Common.Result ret = new Common.Result(false, 0, "");
            if (Session["login_verify_code"] == null || Session["login_verify_code"].ToString().ToUpper().Equals(Code.ToUpper()) == false)
            {
                ret.message = "验证码错误";
                ret.count = 1;
            }
            else
            {
                ret = BLL.LoginUser.Login(UserName, Password);
            }
            new BLL.ManagerLoginLog().Add(new Model.ManagerLoginLog()
            {
                LoginIP = Request.UserHostAddress,
                LoginStatus = ret.count,
                LoginTime = DateTime.Now,
                UserName = UserName
            });
            return UI.Utils.ToJsonString(new
            {
                message = ret.message,
                success = ret.success,
                count = ret.count
            });      
        }
        
        [UI.LoginValidate]
        public RedirectResult LoginOut()
        {
            BLL.LoginUser.LoginOut();
            return Redirect(Url.Action("Login"));
        }

        [UI.LoginValidate]
        public string MenuTree(string WebSiteCode, string id)
        {
            List<object> trees = new List<object>();
            var auth = new BLL.Authority();
            var menus=auth.GetUserMenu(BLL.LoginUser.LoginManager.UserName,WebSiteCode,id);
            foreach (var i in menus)
            {
                trees.Add(new
                {
                    id = i.Code,
                    iconCls = i.IconCls,
                    text = i.Name,
                    state = i.Childs.Count > 0 ? "closed" : null,
                    attributes = new
                    {
                        url = UI.Utils.GetMenuHrefUrl(i)
                    }
                });
            }
            return UI.Utils.ToJsonString(trees);
        }

        [UI.SecurityCheck(false,true)]
        public ActionResult Error(string Title,string Message)
        {
            if (string.IsNullOrEmpty(Title) && string.IsNullOrEmpty(Message))
            {
                var err= Server.GetLastError();
                Title = "内部服务器错误";
                Message = err.Message;

            }
            ViewBag.MsgTitle = Title;
            ViewBag.Message = Message;
            return View();
        }
    }
}
